package in.muton.edushare.grading.panels.studentGrades;

import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.InnerList;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.ItemGradeDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.GradingItem;
import com.edushare.common.vo.ItemGrade;
import com.edushare.common.vo.Users;

public class StudentItemView extends EdusharePanel {
	private static final long serialVersionUID = -1590964554572686798L;
	private StudentMainGradePanel panel = null;
	
	public StudentItemView(String id, EduShareBasePage page,
			final StudentMainGradePanel mainPanel) {
		super(id, page);
		this.panel = mainPanel;
		add(new Label("itemName", new Model<String>() {
			private static final long serialVersionUID = 8850928605405817935L;

			@Override
			public String getObject() {
				if (mainPanel.getActiveMode() == StudentMainGradePanel.VIEW_ITEM_GRADES) {
					return mainPanel.getiGradeModel().getObject()
							.getGradingItemName();
				} else
					return "";
			}
		}));
		
		add(new Label("max", new Model<String>(){
			@Override
			public String getObject(){
				return getMaxMarksString();
			}
			private static final long serialVersionUID = 4229108716054556L;
		}));

		add(new Link<Void>("edit") {
			private static final long serialVersionUID = 3778350553459387502L;

			@Override
			public void onClick() {
				panel.setActiveMode(StudentMainGradePanel.EDIT_ITEM_GRADES);
			}

			@Override
			public boolean isVisible() {
				CourseRoleCheck crc = new CourseRoleCheck(mainPanel.getcInstModel().getObject(), getLoggedInUser());
				return crc.isAdmin()||crc.isTeacher();
			}

		});

		add(new ListView<IModel<Users>>("itemGradesLV", new InnerList<Users>() {
			private static final long serialVersionUID = 1957957473424076743L;

			@Override
			public List<IModel<Users>> getObject() {
				return new CourseDataHandler(mainPanel.getcInstModel())
						.getStudents().getObject();
			}
		}) {
			private static final long serialVersionUID = -3826047740412592426L;

			@Override
			protected void populateItem(final ListItem<IModel<Users>> arg0) {
				arg0.setVisible(isStudentInSearch(arg0.getModelObject().getObject(), panel.getStudentName()));
				arg0.add(new EduShareURLPanel("studentName", arg0
						.getModelObject()));
				arg0.add(new Label("studentGrade", new Model<String>() {
					private static final long serialVersionUID = -2509367356566608330L;
					
					@Override
					public String getObject() {
						if (panel.getiGradeModel() != null) {
							GradingItem g = panel.getiGradeModel().getObject();
							ItemGrade i = new ItemGradeDAO(getORMInterface())
									.getUserGrade(arg0.getModelObject()
											.getObject(), g);
							if (i == null)
								return "Grade not available";
							if (g.getGradeType() == GradingItem.GRADE_LETTER) {
								return i.getGradeKey().getGradeAlphabet();
							} else {
								return "" + i.getMarks();
							}
						}
						return "";
					}
				}));
			}
		});
	}

	public static boolean isStudentInSearch(Users u, String s) {
		if (!StringUtil.isValid(s))
			return true;
		return u.getCompleteUserName().toLowerCase().contains(
				s.trim().toLowerCase());
	}

	@Override
	public boolean isVisible() {
		return (panel.getActiveMode() == StudentMainGradePanel.VIEW_ITEM_GRADES);
	}
	
	public String getMaxMarksString() {
		IModel<GradingItem> item = panel.getiGradeModel();
		double maxMarks = 0.0;
		if(item != null && item.getObject().getMaxMarks()!= null){
			maxMarks = item.getObject().getMaxMarks();
			return " (of "+maxMarks+")";
		}else{
			return "";
		}
	}

	
}
